﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WZ.WebSpider.Serialize
{
    public class ConverModel
    {
        public static T FillModel<T>(GroupCollection regResultGroup) where T : new()
        {
            PropertyInfo[] propertys = (typeof(T)).GetProperties();
            int len = propertys.Length;
            if (len == 0) throw new Exception("Model实体类中不包含任何属性成员");
            T obj = new T();
            for (int i = 0; i < len; i++)
            {
                string columnName = propertys[i].Name;
                if (regResultGroup[columnName].Success)
                {
                    propertys[i].SetValue(obj
                        , Convert.ChangeType(regResultGroup[columnName].Value, propertys[i].PropertyType)
                        , null);
                }
            }
            return obj;
        }
    }
}
